<?php

class WZ_Controller_Router extends Zend_Controller_Plugin_Abstract
{
    public function init()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();
        
        $router_config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
        $router->addConfig($router_config->routes);
    }
}

class WZ_Controller_Plugin_BootstrapLoader extends Zend_Controller_Plugin_Abstract
{
	const CLASS_PLUGIN_APPEND = 'ModuleBootstrap';
	
	/**
	 * Méthode appelée sur l'événement routeShutdown
	 * 
	 * Elle va rechercher une classe {Nomdumodule}ModuleBootstrap dans le répertoire du module
	 * si elle est trouvée celle-ci est chargée et bootstrappée
	 * 
	 * (non-PHPdoc)
	 * @see Zend_Controller_Plugin_Abstract::routeShutdown()
	 */
	public function routeShutdown(Zend_Controller_Request_Abstract $request)
	{
		$module    = $this->getRequest()->getModuleName();
		$moduleDir = Zend_Controller_Front::getInstance()->getModuleDirectory($module);
		
		$moduleBootClass = ucfirst(strtolower($module)) . self::CLASS_PLUGIN_APPEND;
		$moduleBootFile  = $moduleDir . '/' . $moduleBootClass . '.php';
		
		if(file_exists($moduleBootFile)) {
			require_once $moduleBootFile;
			if (!class_exists($moduleBootClass, false)) {
				throw new Zend_Application_Bootstrap_Exception("Class $moduleBootClass not found in $moduleBootFile");
			}

			$bootObject = new $moduleBootClass('');
			if (!$bootObject instanceof Zend_Application_Bootstrap_BootstrapAbstract) {
				throw new Zend_Application_Bootstrap_Exception("Class $moduleBootClass must inherit from Zend_Application_Bootstrap_BootstrapAbstract");
			}
			
			$bootObject->bootstrap(); // load it !
		}		
	}
}